package com.changpei.core.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 *
 * @author Way on 2021/9/9
 */
@Configuration
public class FollowMqConfig {

    @Autowired
    private RabbitInfoConfig rabbitInfoConfig;

    /**
     * 创建队列
     * @return
     */
    @Bean("UserFollowQueue")
    public Queue userFollowQueue() {
        return new Queue(rabbitInfoConfig.getQueue().get("follow"));
    }

    /**
     * 创建交换机
     * @return
     */
    @Bean("UserFollowExchange")
    public Exchange userFollowExchange() {
        return new DirectExchange(rabbitInfoConfig.getExchange().get("follow"), true, false);
    }

    /**
     * 队列绑定交换机
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding userFollowQueueExchange(@Qualifier("UserFollowQueue") Queue queue, @Qualifier("UserFollowExchange") Exchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with(rabbitInfoConfig.getRouting().get("follow"))
                .noargs();
    }
}
